<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证邮箱唯一性</title>
    <link rel="stylesheet" href="/assets/bootstrap/dist/css/bootstrap.min.css">
    <style type="text/css">
        p:not(:empty) {
            padding: 15px;
        }
        .container {
            padding-top: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form-group">
            <label for="">邮箱地址</label>
            <input type="email" class="form-control" placeholder="请输入邮箱地址" id="email">
        </div>
        <!-- 错误 bg-danger 正确 bg-success -->
        <p id="info"></p>
    </div>
    <script src="/js/ajax.js"></script>
    <script>
        var emailInp = document.getElementById('email');
        var info = document.getElementById('info');

        emailInp.onblur = function () {
            var email = this.value;
            var reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
            
            if(!reg.test(email)){
                info.innerHTML = '请输入正确的邮箱地址';
                info.className = 'bg-danger';
                return;
            }
            //向服务器端发送请求
            ajax({
                type: 'get',
                url: 'http://localhost:3000/verifyEmailAdress',
                data:{
                    email:email
                },
                success:function(result){
                    console.log(result);
                    info.innerHTML = result.message;
                    info.className = 'bg-success';
                },
                error:function(result){
                    console.log(result);
                    info.innerHTML = result.message;
                    info.className = 'bg-danger';
                }
            });
          }
    </script>
</body>
</html>